﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Tszy.Unlimited.Base.Extensions;

/// <summary>
/// 
/// </summary>
public static class ConfigurationExtensions
{
    /// <summary>
    /// 获得配置信息。
    /// </summary>
    /// <param name="_"></param>
    /// <param name="isDevelopment"></param>
    /// <param name="configPath">配置文件路径。</param>
    /// <returns></returns>
    public static IConfiguration GetConfiguration(this IServiceCollection _, bool isDevelopment, string configPath = "")
    {
        if (string.IsNullOrWhiteSpace(configPath))
        {
            var settingFile = isDevelopment ? "appsettings.Development.json" : "appsettings.json";
            configPath = $"Configs/{settingFile}";
        }

        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile(configPath, true, true)
            .Build();

        return configuration;
    }
}